<template>
  <c-select
    v-bind="$attrs"
    v-on="$listeners"
    :value="value"
    :options="options"
    @change="hangleChange"
    :props="props"
  ></c-select>
</template>

<script>
import { mapGetters } from 'vuex'
import { addressCut } from '@vimi/utils-tool'
export default {
  components: {},
  props: {
    value: String,
    indCode: {
      type: String,
      default: '',
    },
  },
  model: {
    prop: 'value',
    event: 'change',
  },
  data() {
    return {
      tableName: 'industrial_package_agency_view',
      loading: false,
      options: [],
      props: {
        label: 'load_name',
        value: 'industrial_package_id',
      },
    }
  },
  computed: {
    ...mapGetters(['user']),
    queryAddressCode() {
      return this.user.queryAddressCode
    },
  },
  watch: {},
  created() {
    this.getData()
  },
  mounted() {},
  methods: {
    getData() {
      this.loading = true
      this.$api
        .getData(
          this.$serviceName,
          this.tableName,
          {
            supply_address_code: addressCut(this.queryAddressCode),
            'ind_code.eq': this.indCode,
            'is_industrial_package.eq': 1,
            groupFields: 'industrial_package_id',
          },
          {
            create_time: 'desc',
          }
        )
        .then(res => {
          const data = res?.data ?? []
          this.options = data
        })
        .finally(() => {
          this.loading = false
        })
    },
    hangleChange(val) {
      const select = this.options.find(item => item.industrial_package_id === val) || {}
      this.$emit('select', select.load_name, select)
    },
  },
}
</script>

<style lang="scss" scoped></style>
